<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="bear" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item>编辑子部门</el-dropdown-item>
                        <el-dropdown-item>删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- :default-expand-all="true" 默认展开所有子节点 -->
      </el-card>
      <el-dialog title="添加或编辑" :visible.sync="showDialog" :close-on-press-escape="false" :close-on-click-modal="false">
        <dep-dialog :id="curId" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import depDialog from './depDialog.vue'
export default {
  components: { depDialog },
  data() {
    return {
      // 依赖一份树形数据
      showDialog: false,
      curId: '',
      list: [],
      defaultProps: {
        // children: 'children', // 控制子节点集合名称 默认children
        label: 'name' // 控制所有数据名称 默认label
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // shift() 删除数组中的第一个数据,返回值是删除的数据,取出数组中的第一
        const company = res.data.depts.shift()
        console.log(company, 'company')
        console.log(res.data.depts, 'getDepartments()')
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
